package com.aivin.mobileApi;

import com.aivin.entity.*;
import com.aivin.service.FileDetailService;
import com.aivin.utils.MyStringUtils;
import com.aivin.utils.MyValidateUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 获取参与搭配的数据的信息
 */
@Controller
@RequestMapping("/fileDataApi")
public class FileDeatilController {

    public static String background01="background01";
    public static String bowtie13="bowtie13";
    public static String brow18="brow18";
    public static String eye15="eye15";
    public static String face06="face06";
    public static String glasses19="glasses19";
    public static String hairfront20="hairfront20";
    public static String mouth17="mouth17";
    public static String nose16="nose16";
    public static String scarffront14="scarffront14";
    public static String shirtfront07="shirtfront07";
    public static String suitback04="suitback04";
    public static String suitfront12="suitfront12";
    public static String tie10="tie10";




    @Autowired
    public    FileDetailService fileDetailService ;




    /**
     * 获取文件的 预览信息。信息大纲。
     *  http://192.168.0.102:8081/findYou/fileDataApi/getFilePreInfos/android/boy
     * @param whichSys  ios or android
     * @param mSex boy or girl
     * @return  jsonStr  of FileAllPreviews
     */
    @RequestMapping(value = "/getFilePreInfos/{whichSys}/{mSex}",   method = {RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public  String  getFilePreInfos(   @PathVariable String whichSys , @PathVariable String mSex   ) {

        List<FileAllPreview> list = fileDetailService.getFilePreInfos(mSex);
        list= sortMyList(list) ;//排序

        if("ios".equals(whichSys)){
            List<FileAllPreview2_ios> listNew = new ArrayList<FileAllPreview2_ios>() ;
            for(FileAllPreview bean : list){
                FileAllPreview2_ios beanNew =new FileAllPreview2_ios();
                beanNew.setmId(bean.getmId());
                beanNew.setmDes(bean.getmDes());
                beanNew.setmContent(getContentInfo_ios(bean.getmContent()));
                beanNew.setmIcon(bean.getmIcon());
                beanNew.setmIconH(bean.getmIconH());
                beanNew.setmDataHost(bean.getmDataHost());
                beanNew.setmPart(bean.getmPart());
                beanNew.setmSex (bean.getmSex());
                beanNew.setmColorInfo(bean.getmColorInfo());
                listNew.add(beanNew) ;
            }//

            //过滤掉不需要显示的属性
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
            filter.getExcludes().add("mId");
            String jsonStr = JSONObject.toJSONString(listNew ,filter);

            return jsonStr;
        }else{
            List<FileAllPreview2_android> listNew = new ArrayList<FileAllPreview2_android>() ;
            for(FileAllPreview bean : list){
                FileAllPreview2_android beanNew =new FileAllPreview2_android();
                beanNew.setmId(bean.getmId());
                beanNew.setmDes(bean.getmDes());
                beanNew.setmContent(getContentInfo_android(bean.getmContent()));
                beanNew.setmIcon(bean.getmIcon());
                beanNew.setmIconH(bean.getmIconH());
                beanNew.setmDataHost(bean.getmDataHost());
                beanNew.setmPart(bean.getmPart());
                beanNew.setmSex (bean.getmSex());
                beanNew.setmColorInfo(bean.getmColorInfo());
                listNew.add(beanNew) ;
            }//

            //过滤掉不需要显示的属性
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
            filter.getExcludes().add("mId");
            String jsonStr = JSONObject.toJSONString(listNew ,filter);

            return jsonStr;
        }//end else

    }




    private  List<FileAllPreview>  sortMyList(List<FileAllPreview> list){
        Collections.sort(list, new Comparator<FileAllPreview>() {
            @Override
            public int compare(FileAllPreview bean1, FileAllPreview bean2) {
                // 排序规则
                int a = bean1.getmId();
                int b = bean2.getmId();
                return a - b;

            }
        });
        return  list  ;
    }

    public List<String> getContentInfo_ios(String msg){
        List<String> result = new ArrayList<String>();
        String[] array= msg.split("_") ;
        for(String str: array){
            result.add(str) ;
        }
        return  result;
    }


    public List<ChineaseDesOfStyle_android> getContentInfo_android(String msg){
        // 注意: msg是已经排好序了的
        List<ChineaseDesOfStyle_android> result = new ArrayList<ChineaseDesOfStyle_android>();
        String[] array= msg.split("_") ;
        int size = array.length ;
        for(int i=0 ;i<size ;i++){
            ChineaseDesOfStyle_android chineaseDesOfStyle_android = new ChineaseDesOfStyle_android();
            chineaseDesOfStyle_android.setmDes(array[i]);
            chineaseDesOfStyle_android.setmStyle(getTempStyleByChineaseIndex(i));
            result.add(chineaseDesOfStyle_android) ;
        }
        return  result;
    }


    /**android专用**/
    private  String getTempStyleByChineaseIndex(int index ){
        index =index+1;
        if(index<10){
         return  "k0"+index;
        }else{
            return  "k"+index;
        }
    }

    public  String  getFileDetailDatasSkinOrSize(   @PathVariable String whichSys , @PathVariable String mSex  ,@PathVariable String mPart ) {
       // System.out.println("mPart="+mPart);
        List<FileDetailTV1> list = fileDetailService.getFileDetailDatas(mSex,"face06");
        if(MyValidateUtil.isEmpty(list)){
            return  "noData" ;
        }
//        List<FileDetailTV1> listNew =   new ArrayList<FileDetailTV1>() ;
        // 肤色 和体型 特殊处理。  肤色 有 face的 颜色决定 。  体型 由
        if("肤色".equals(mPart)){

            for (int i = 0; i < list.size() - 1; i++) {
                for (int j = list.size() - 1; j > i; j--) {
                    if(list.get(j).getfColor().equals(list.get(i).getfColor())){
                        //保存相同的bean
//                        listNew.add(list.get(j)) ;
                        list.remove(j);
                    }
                }
            }//


        }else if ("体型".equals(mPart)){
            for (int i = 0; i < list.size() - 1; i++) {
                for (int j = list.size() - 1; j > i; j--) {
                    if(list.get(j).getfSize().equals(list.get(i).getfSize())){
                        //保存相同的bean
//                        listNew.add(list.get(j)) ;
                        list.remove(j);
                    }
                }
            }//
        }


        //http://findstar-v1.gz.bcebos.com/boy_face06_zs_k01_m01_c01_10.zip
        for(FileDetailTV1 bean : list){
            if("肤色".equals(mPart)){
                bean.setfName("肤色");
                bean.setfPart("肤色");
                bean.setfDes("肤色"+bean.getfColor());
                bean.setfIcon("http://findstar-v1.gz.bcebos.com/"+bean.getfSex()+"_"+bean.getfPart()+"_"+bean.getfColor()+".png");
            }else{
                bean.setfName("体型");
                bean.setfPart("体型");
                bean.setfDes("体型"+bean.getfSize());
                bean.setfIcon("http://findstar-v1.gz.bcebos.com/"+bean.getfSex()+"_"+bean.getfPart()+"_"+bean.getfSize()+".png");
            }
        }



        //过滤掉不需要显示的属性
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
        filter.getExcludes().add("fId");
        filter.getExcludes().add("fSex");
        filter.getExcludes().add("fSize");
        filter.getExcludes().add("fShow");
        filter.getExcludes().add("fStyle");
        filter.getExcludes().add("fLevel");
        filter.getExcludes().add("fIsCartoon");
//        filter.getExcludes().add("fPart");
        filter.getExcludes().add("fIndex");
        filter.getExcludes().add("fColor");
        String jsonStr = JSONObject.toJSONString(list ,filter);
        return jsonStr;

//        }//end else
    }//




    /***
     *  http://192.168.0.102:8081/findYou/fileDataApi/getFileDetailDatas/android/boy/face06
     *  http://192.168.0.102:8081/findYou/fileDataApi/getFileDetailDatas/android/boy/hairfront20
     *  http://192.168.0.102:8081/findYou/fileDataApi/getFileDetailDatas/android/boy/shirtfront07
     *   http://192.168.0.102:8081/findYou/fileDataApi/getFileDetailDatas/android/boy/体型
     *  获取某一类型的 文件列表
     * @param whichSys ios or  android
     * @param mSex  boy  or girl
     * @param mPart   face06 、 hairfront20 and so on ...
     * @return
     */
    @RequestMapping(value = "/getFileDetailDatas/{whichSys}/{mSex}/{mPart}",   method = {RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public  String  getFileDetailDatas(   @PathVariable String whichSys , @PathVariable String mSex  ,@PathVariable String mPart ) {


        if( "肤色".equals(mPart)||"体型".equals(mPart)){
          return  getFileDetailDatasSkinOrSize(whichSys ,mSex ,mPart) ;
        }else{

            List<FileDetailTV1> list = fileDetailService.getFileDetailDatas(mSex,mPart);
            if(MyValidateUtil.isEmpty(list)){
                return  "noData" ;
            }

            List<FileDetailTV1> listNew = getFileDetailTV1_3(list,  whichSys ,    mSex  ,   mPart) ;

            //过滤掉不需要显示的属性
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
            filter.getExcludes().add("fId");
            filter.getExcludes().add("fShow");
            filter.getExcludes().add("fLevel");


            if("ios".equals(whichSys)){
                filter.getExcludes().add("fStyle");
                filter.getExcludes().add("fPart");
                filter.getExcludes().add("fSex");
                filter.getExcludes().add("fSize");
                filter.getExcludes().add("fIndex");
                filter.getExcludes().add("fColor");
                filter.getExcludes().add("fIsCartoon");
            }




            String jsonStr = JSONObject.toJSONString(listNew ,filter);
            return jsonStr;

        }

    }//


    /**
     * 返回结果就是分三种情况:
     * 1、五官 ，返回每个文件的
     * 2、头发  ， 返回k01开头的所有 ， 下标从 10 到17 的
     * 3  其他服饰  返回 k01 到 kn 的 ，下标都是 10 的
     */
    private  List<FileDetailTV1> getFileDetailTV1_3(    List<FileDetailTV1> list ,String whichSys ,  String mSex  , String mPart){
        List<FileDetailTV1> listNew = new ArrayList<FileDetailTV1>();
        //五官 .
        if(face06.equals(mPart) ||nose16.equals(mPart)||mouth17.equals(mPart)||eye15.equals(mPart)||brow18.equals(mPart)){
            listNew =list;

        }else if(hairfront20.equals(mPart)){
            //头发
            for(FileDetailTV1 bean : list){
                if(bean.getfStyle().equals("k01")){
                    listNew.add(bean) ;
                }//
            }

        }else{
            //其他服饰
            Set<String> tempSet= new HashSet<String>();
            for(FileDetailTV1 bean : list){
                tempSet.add(bean.getfStyle());
            }

            StringBuffer sb = new StringBuffer() ;
            FileDetailTV1 beantemp =list.get(0);
            for(String str:tempSet){
                sb.delete(0,sb.length());
                sb.append(beantemp.getfSex());
                sb.append("_");
                sb.append(beantemp.getfPart());
                sb.append("_");
                sb.append(beantemp.getfIsCartoon());
                sb.append("_");
                sb.append(str);
                sb.append("_");
                sb.append("m01_c01_10");
                for(FileDetailTV1 bean : list){

                    if(bean.getfName().equals(sb.toString())){
                        listNew.add(bean) ;
                    }
                }
            }//
        }//

        return  listNew;
    }



    /***
     *  http://192.168.0.102:8081/findYou/fileDataApi/getFileDatasByType/eye
     * @param fileType eye 、mouth 等...
     * @return 根据类型获取 搭配文件的信息。 json数组
     */
    @RequestMapping(value = "/getFileDatasByType/{fileType}",
            method = {RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    @Deprecated
    public  String  getFileDatasByType(   @PathVariable String fileType   ) {

        List<FileDetailBean> list = fileDetailService.getFileDatasByType(fileType);
        String  jsonStr= JSONObject.toJSONString(list);

        return jsonStr;
    }





    /***
     *  http://192.168.0.119:8081/findYou/fileDataApi/addFileDetailBean
     * 内部使用。 内部添加 文件数据信息。
     * @return
     */
    @RequestMapping(value = "/addFileDetailBean",
            method = {RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    @Deprecated
    public  String  addFileDetailBean(      ) {

        for(int j=1;j<=8;j++){
            List<FileDetailBean>  list =getFileDeatilDatas(j) ;
            for(FileDetailBean bean :list){
                int resultState=  fileDetailService.addFileDetailBean( bean ) ;
                System.out.println(""+resultState);
            }

            List<FileDetailBean>  list2=   getFileDeatilDatasLevel1(j);
            for(FileDetailBean bean :list2){
                int resultState=  fileDetailService.addFileDetailBean( bean ) ;
                System.out.println(""+resultState);
            }
        }

        return "helloaivin";
    }


    public    List<FileDetailBean> getFileDeatilDatasLevel1(int type){
        List<FileDetailBean> list = new ArrayList<FileDetailBean>();
        for(int i=1;i<=1;i++){
            System.out.println("getFileDeatilDatasLevel1 " );
            FileDetailBean bean = new FileDetailBean();
            //bean.setFileId(1);
            bean.setFileType("face");
            bean.setFileLevel("L1");//级别1
            bean.setFileStyle("K"+type);//款式1
            bean.setFileIndex(i);

            bean.setFileColor("C1");//颜色1
            bean.setFileSize("M1");
            bean.setFileSex("boy");
            bean.setIsCartoon("false");//卡通 或真实
            bean.setRegTime(MyStringUtils.getCurrentTimeStr());
            bean.setExpired("false");

            bean.setFileName(getFileName(bean));
            bean.setFilePath("http://findstar-v1.gz.bcebos.com/"+bean.getFileName()+".data");
            bean.setFileIcon("http://findstar-v1.gz.bcebos.com/"+bean.getFileName()+".png");
            list.add(bean);
        }


        return  list ;
    }



    public    List<FileDetailBean> getFileDeatilDatas(int type){
        List<FileDetailBean> list = new ArrayList<FileDetailBean>();
        for(int i=1;i<=7;i++){
            FileDetailBean bean = new FileDetailBean();
            //bean.setFileId(1);
            bean.setFileType("face");
            bean.setFileLevel("L2");//级别1
            bean.setFileStyle("K"+type);//款式1
            bean.setFileIndex(i);

            bean.setFileColor("C1");//颜色1
            bean.setFileSize("M1");
            bean.setFileSex("boy");
            bean.setIsCartoon("false");//卡通 或真实
            bean.setRegTime(MyStringUtils.getCurrentTimeStr());
            bean.setExpired("false");

            bean.setFileName(getFileName(bean));
            bean.setFilePath("http://findstar-v1.gz.bcebos.com/"+bean.getFileName()+".data");
            bean.setFileIcon("http://findstar-v1.gz.bcebos.com/"+bean.getFileName()+".png");
            list.add(bean);
        }

        System.out.println("list"+list.size());
        return  list ;
    }



    @Deprecated
    public   String getFileName( FileDetailBean bean ){
        StringBuffer sb = new StringBuffer() ;
        sb.append(bean.getFileSex());
        sb.append("_");
        sb.append(bean.getFileType());
        sb.append("_");
        //
        if("true".equals(bean.getIsCartoon())){
            sb.append("ct");
        }else{
            sb.append("zs") ;
        }
        sb.append("_");

        sb.append(bean.getFileSize());
        sb.append("_");
        sb.append(bean.getFileColor());
        sb.append("_");
        sb.append(bean.getFileStyle());
        sb.append("_");
        sb.append(bean.getFileLevel());
        sb.append("_");
        sb.append(bean.getFileIndex());

        return  sb.toString() ;
    }
}
